<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="景区名称" prop="areaName">
          <el-input
            v-model="queryParams.areaName"
            placeholder="请输入景区名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="景区地点" prop="location">
          <el-input
            v-model="queryParams.location"
            placeholder="请输入景区地点"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="开放时间" prop="openTime">
          <el-input
            v-model="queryParams.openTime"
            placeholder="请输入开放时间"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="门票价格" prop="ticketPrice">
          <el-input
            v-model="queryParams.ticketPrice"
            placeholder="请输入门票价格"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>
      </el-form>
  
      <el-table v-loading="loading" :data="scenicareaList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="景区编号" align="center" prop="areaId" />
        <el-table-column label="景区名称" align="center" prop="areaName" />
        <el-table-column label="景区地点" align="center" prop="location" />
        <el-table-column label="景区描述" align="center" prop="description" />
        <el-table-column label="开放时间" align="center" prop="openTime" />
        <el-table-column label="门票价格" align="center" prop="ticketPrice" />
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改【请填写功能名称】对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="景区名称" prop="areaName">
            <el-input v-model="form.areaName" placeholder="请输入景区名称" />
          </el-form-item>
          <el-form-item label="景区地点" prop="location">
            <el-input v-model="form.location" placeholder="请输入景区地点" />
          </el-form-item>
          <el-form-item label="景区描述" prop="description">
            <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="开放时间" prop="openTime">
            <el-input v-model="form.openTime" placeholder="请输入开放时间" />
          </el-form-item>
          <el-form-item label="门票价格" prop="ticketPrice">
            <el-input v-model="form.ticketPrice" placeholder="请输入门票价格" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listScenicarea, getScenicarea, delScenicarea, addScenicarea, updateScenicarea } from "@/api/tourss/scenicarea/area";
  
  export default {
    name: "Scenicarea",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        scenicareaList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          areaName: null,
          location: null,
          description: null,
          openTime: null,
          ticketPrice: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          areaName: [
            { required: true, message: "景区名称不能为空", trigger: "blur" }
          ],
          location: [
            { required: true, message: "景区地点不能为空", trigger: "blur" }
          ],
          description: [
            { required: true, message: "景区描述不能为空", trigger: "blur" }
          ],
          openTime: [
            { required: true, message: "开放时间不能为空", trigger: "blur" }
          ],
          ticketPrice: [
            { required: true, message: "门票价格不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        listScenicarea(this.queryParams).then(response => {
          this.scenicareaList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          areaId: null,
          areaName: null,
          location: null,
          description: null,
          openTime: null,
          ticketPrice: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.areaId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加【请填写功能名称】";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const areaId = row.areaId || this.ids
        getScenicarea(areaId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改【请填写功能名称】";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.areaId != null) {
              updateScenicarea(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addScenicarea(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const areaIds = row.areaId || this.ids;
        this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + areaIds + '"的数据项？').then(function() {
          return delScenicarea(areaIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/scenicarea/export', {
          ...this.queryParams
        }, `scenicarea_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>